package test21.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTCP02Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket accept = serverSocket.accept();
        //读取数据
        InputStream inputStream = accept.getInputStream();
        byte[] buf = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(buf)) != -1){
            System.out.println(new String(buf,0,readLen));
        }
        //获取socket相关联的输出流
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("hello,client".getBytes());
        // 设置结束标记
        accept.shutdownOutput();
        //关闭流和socket
        outputStream.close();
        inputStream.close();
        accept.close();
        serverSocket.close();
    }
}
